package cn.itsource.hrm.service.impl;


import cn.itsource.hrm.domain.Login;
import cn.itsource.hrm.domain.dto.LoginUserInfo;
import cn.itsource.hrm.exception.GlobalException;
import cn.itsource.hrm.feign.clients.SystemEmpControllerClient;
import cn.itsource.hrm.mapper.LoginMapper;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.AssertUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


// 通过数据库查询 用户的信息
@Slf4j
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private LoginMapper mapper;

    @Autowired
    private SystemEmpControllerClient client;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 空对象，用用户名查数据信息
        Login login = new Login();
        login.setUsername(username);

        Login mysqlLogin = mapper.selectOne(login);

        AssertUtils.isnotNull("用户不存在",mysqlLogin);
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        User user = null;
        if(mysqlLogin.getType().intValue() ==Login.SYSTMEM_TYPE){
            // 通过 Feign去调用system服务,获取用户的权限
            AjaxResult ajaxResult = client.selectUserInfo(mysqlLogin.getId());
            if(!ajaxResult.isSuccess()){
                log.info(ajaxResult.getMessage());
                throw new GlobalException("Authority 认证登录失败！ ");
            }
            // feign接口调用的结果默认封装的对象 LinkedHashMap
            String jstr = JSON.toJSONString(ajaxResult.getResultObj());
            LoginUserInfo userInfo = JSON.parseObject(jstr, LoginUserInfo.class);
            // 封装UserDetails
            List<String> premis = userInfo.getPremis();
            authorities = premis.stream()
                    .map(p->{
                        return new SimpleGrantedAuthority(p);
                    }).collect(Collectors.toList());

             user = new User(JSON.toJSONString(userInfo),mysqlLogin.getPassword(),authorities);

        }else{
            // 前台用户没有权限
             user = new User(username,mysqlLogin.getPassword(),authorities);

        }

        return user;
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean flag = encoder.matches("123", "$2a$10$qVrG0QhkBPzsAvSJx4NDf.1OiR1YgGsJDFFOKcHj1rgXDDCxs8hiu");
        System.out.println(flag);

    }
}


